﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Web;
using IntraVision.Web;
using IntraVision.Web.Security;
using IntraVision.Web.Filter;
using Model;

public class NotarialFormReportController : ReportController<NotarialServiceDataContext>
{
    /*
    protected override IQueryable<Order> CustomFilter(NotarialServiceDataContext dc, IQueryable<Order> query)
    {
        return query.Where(q => !q.IsNotOnForm && q.NotarialFormNumber != null && q.NotarialFormNumber.Count > 0 && q.Finished.HasValue);
    }

    protected override IQueryable ConvertQueryToResult(NotarialServiceDataContext dc, IQueryable<Order> query)
    {
        return (from q in query    
                join nf in dc.NotarialFormNumber on q.Id equals nf.OrderId
                select new OrderForm
                {
                       Id = q.Id,
                       FormNumber = nf.Name,
                       Finished = q.Finished.Value,
                       NotarialRegisterId = q.NotarialRegisterId,
                       NotarialRegisterRow = q.NotarialRegisterRow,
                       NotarialRegisterName = q.NotarialRegister.Name,
                       NotarialActionName = q.NotarialAction.Name,
                       NotarialActionFullName = q.NotarialAction.FullName,
                       NotarialActionId = q.NotarialActionId
                });        
    }

    public override void Default()
    {        
        List();     
    }

    protected override IQueryable<Order> Search(IQueryable<Order> query)
    {
        int oid;
        var searchStr = State["tb_search"] != null ? State["tb_search"].Trim() : "";
        if (!string.IsNullOrEmpty(searchStr))
        {
            var search = String.Format("%{0}%", searchStr.Trim().ToLower());
            //поиск по идентификатору заявки
            if ((Int32.TryParse(searchStr, out oid)) && oid > 0)
                return query.Where(q => q.Id == oid || q.NotarialFormNumber.Any(nf => SqlMethods.Like(nf.Name, search)));
            return query.Where(q => q.NotarialFormNumber.Any(nf => SqlMethods.Like(nf.Name, search)));
        }
        return query;
    }

    protected override void CreateFilterConditions(NotarialServiceDataContext dc)
    {
        base.CreateFilterConditions(dc);

        Filter = new Filter<Order>();
        var fc = Filter.Conditions;
        fc.Add(new NumericFilterCondition<Order>("id", "Номер заявки"));
        fc.Add(new DateFilterCondition<Order>("finished", "Дата выдачи"));
        fc.Add(new TreeFilterCondition<Order>("NotarialAction.Path", "Нотариальное действие", (from c in dc.NotarialActions orderby c.FullName select new { Path = c.Path, FullName = c.FullName })));
        fc.Add(new SelectFilterCondition<Order>("notarialregisterid", "Номер реестра", (from n in dc.NotarialRegisters orderby n.Id select n)));
        fc.Add(new NumericFilterCondition<Order>("notarialregisterrow", "Номер записи реестра"));
        fc.Add(new ManualFilterCondition<Order>("manual", "Дополнительно"));
    }*/
         
}
